#ifndef MOUNTAINRECORDMODEL_H
#define MOUNTAINRECORDMODEL_H

#include <QObject>
#include <QAbstractListModel>
#include <QDateTime>
#include <QStringList>

// 登山记录数据结构
struct MountainRecord {
    int id;
    QString mountainName;      // 山名
    QString location;           // 地点
    double latitude;           // 纬度
    double longitude;          // 经度
    QDateTime date;            // 登山日期
    QStringList photos;        // 照片路径列表
    QString description;       // 描述
    float altitude;            // 海拔高度
    float distance;            // 登山距离
    int duration;              // 登山时长（分钟）
    QString weather;           // 天气
    int difficulty;            // 难度等级 1-5
};

class MountainRecordModel : public QAbstractListModel
{
    Q_OBJECT

public:
    enum RecordRoles {
        IdRole = Qt::UserRole + 1,
        MountainNameRole,
        LocationRole,
        LatitudeRole,
        LongitudeRole,
        DateRole,
        PhotosRole,
        DescriptionRole,
        AltitudeRole,
        DistanceRole,
        DurationRole,
        WeatherRole,
        DifficultyRole
    };

    explicit MountainRecordModel(QObject *parent = nullptr);

    // QAbstractListModel 接口
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QHash<int, QByteArray> roleNames() const override;

    // QML 暴露的方法
    Q_INVOKABLE void addRecord(const QString &mountainName, const QString &location,
                              double latitude, double longitude, const QStringList &photos,
                              const QString &description, float altitude, float distance,
                              int duration, const QString &weather, int difficulty);
    Q_INVOKABLE void removeRecord(int id);
    Q_INVOKABLE void updateRecord(int id, const QString &mountainName, const QString &location,
                                 double latitude, double longitude, const QStringList &photos,
                                 const QString &description, float altitude, float distance,
                                 int duration, const QString &weather, int difficulty);
    Q_INVOKABLE QVariantList getAllRecords() const;
    Q_INVOKABLE QVariantMap getRecordById(int id) const;

    // QML 暴露属性
    Q_PROPERTY(int totalRecords READ totalRecords NOTIFY totalRecordsChanged)
    Q_PROPERTY(float totalDistance READ totalDistance NOTIFY totalDistanceChanged)
    Q_PROPERTY(int totalDuration READ totalDuration NOTIFY totalDurationChanged)

    int totalRecords() const;
    float totalDistance() const;
    int totalDuration() const;

signals:
    void totalRecordsChanged();
    void totalDistanceChanged();
    void totalDurationChanged();

private:
    QList<MountainRecord> m_records;
    int m_nextId;

    void initializeDefaultRecords();
    void updateStatistics();
};

#endif // MOUNTAINRECORDMODEL_H
